﻿using System;
using WeDonekRpc.Helper;
using WeDonekRpc.PipeClient.Config;
using WeDonekRpc.PipeClient.Interface;
namespace WeDonekRpc.PipeClient.Log
{
    internal class IoLogSystem
    {
        private static readonly string _LogGroup = "PipeClientLog";
        private static readonly string _SendGroup = "SendException";

        private static readonly string _ReceiveGroup = "ReceiveException";


        private static readonly string _ConnectGroup = "ConnectException";

        private static readonly string _LeaveUnsed = "LeaveUnsedEvent";
        public static void AddSendLog (Exception e, Client.PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_SendGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Args.Add("Host", client.Host);
                error.Save(_LogGroup);
            }
        }
        public static void AddReceiveLog (Exception e, Client.PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_ReceiveGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Args.Add("Host", client.Host);
                error.Save(_LogGroup);
            }
        }

        internal static void AddLeaveUnusedLog (IClient client, int time)
        {
            if (PipeLogConfig.CheckIsRecord(_LeaveUnsed, out LogGrade grade))
            {
                new LogInfo("链接闲置释放事件!", "闲置时间超过：" + Tools.FormatTime(time - client.LastTime), _LogGroup, grade)
                {
                    { "PipeName", client.PipeName },
                    { "Host", client.Host}
                }.Save();
            }
        }
        internal static void AddConnectLog (Exception e, Client.PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_ConnectGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Args.Add("Host", client.Host);
                error.Save(_LogGroup);
            }
        }
    }
}
